/*
 * @Description: 全宗reducer
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2023-02-25 19:16:10
 */

import { delItemById, insertItemAfter } from '@/utils/list';
import {
  ADD_CONDITION_ITEM,
  ADD_SCOPE_ITEM,
  CLEAR_CONDITION_LIST,
  DELETE_LAST_CONDITION_ITEM,
  DELETE_SCOPE_ITEM,
  SET_FIELD_LIST,
  SET_FOND_LIST,
  SET_DB_NAME
} from '../constants';

export const AdvanceReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 加载表格数据及相关数据
     * @param {Array} data.results 表格数据
     * @param {Number} TotalCount 数据总数量
     * @return {*}
     */
    case SET_FOND_LIST: {
      return {
        ...state,
        fondsList: action.data
      };
    }
    /* dbName Change */
    case SET_DB_NAME: {
      return {
        ...state,
        dbName: action.data
      };
    }
    // 增加选择范围项目
    case ADD_SCOPE_ITEM:
      console.log('ADD', action.id);
      console.log(state.scopeList);
      return {
        ...state,
        scopeList: insertItemAfter(action.data, [...state.scopeList])
      };

    /**
     * @description: 删除选择范围项目
     * @param {Number} id 删除选择范围项目的ID
     */
    case DELETE_SCOPE_ITEM: {
      const scopeList = delItemById(action.id, state.scopeList);
      console.log('red', scopeList);
      return { ...state, scopeList };
    }

    //   设置字段列表
    case SET_FIELD_LIST: {
      const fieldList = action.data;
      console.log('field', action);
      return {
        ...state,
        fieldList
      };
    }

    // 增加选择范围项目
    case ADD_CONDITION_ITEM:
      return {
        ...state,
        conditionList: insertItemAfter(action.data, [...state.conditionList])
      };

    // 增加选择范围项目
    case DELETE_LAST_CONDITION_ITEM: {
      const _list = [...state.conditionList];
      _list.pop();
      return {
        ...state,
        conditionList: _list.map((item) => item)
      };
    }

    // 增加选择范围项目
    case CLEAR_CONDITION_LIST:
      return {
        ...state,
        conditionList: []
      };

    // /**
    //  * @description: 添加全宗
    //  * @param {Object} data 新增的全宗
    //  */
    // case AdvanceConstants.ADD_FONT:
    //   return { ...state, list: insertItem(action.data, state.list), dialog: { visible: false }};

    // /**
    //  * @description: 编辑全宗
    //  * @param {Object} data 编辑的全宗
    //  */
    // case AdvanceConstants.EDIT_FONT:

    //   console.log('fond edit1', action.data, state.list);
    //   return {
    //     ...state,
    //     list: editItemById(action.data.id, action.data, state.list),
    //     editDialog: { visible: false, current: {}}
    //   };

    // // 显示添加弹窗
    // case AdvanceConstants.SHOW_DIALOG:
    //   return { ...state, dialog: { visible: true }};

    //   // 显示编辑弹窗
    // case AdvanceConstants.SHOW_EDIT_DIALOG:
    //   return { ...state, editDialog: { visible: true, current: action.data }};

    // // 隐藏弹窗
    // case AdvanceConstants.HIDE_DIALOG:
    //   return {
    //     ...state,
    //     dialog: { visible: false },
    //     editDialog: { visible: false, current: {}}
    //   };

    default:
      console.log('untouched', action);
      return state;
  }
};
